import 'package:meta/meta.dart';
import 'package:drm/base/appurl.dart';
import 'package:common_utils/common_utils.dart';
import 'package:drm/util/SpUtils.dart';

@immutable
abstract class IpsettingEvent {}

class IpSettingGetEvent extends IpsettingEvent {
  getLocalSetting() {
    return SpUtils.getAppUrl();
  }
}


class NullEvent extends IpsettingEvent {

}

class IpSettingSaveEvent extends IpsettingEvent {
  AppIpPort appUrl;

  String ipError;
  String portError;
  bool isGood;

  IpSettingSaveEvent({this.appUrl, this.ipError, this.portError, this.isGood});

  bool check() {
    ipError = null;
    portError = null;
    if (appUrl.ip.isEmpty || !RegexUtil.isIP(appUrl.ip)) {
      ipError = '请输入正确的ip地址';
      return false;
    }
    bool portResult = false;
    if (appUrl.port.isEmpty) {
      portResult = false;
    } else {
      try {
        int.parse(appUrl.port);
        portResult = true;
      } catch (e) {
        portResult = false;
      }
    }

    if (portResult == false) {
      portError = '请输入正确的port地址';
    }

    return portResult;
  }

  Future<bool> saveSetting() async {
    bool result = check();
    if (result) {
      result = await SpUtils.saveAppUrl(appUrl);
    }
    return result;
  }
}
